package org.brazilo.movi.miaamiko;

import java.io.IOException;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class LicaoActivity extends Activity {
	TextView titulo;
	WebView webconteudo;
	String licaoid;
	
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.licao);
        
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            licaoid = extras.getString("LICAOXML");
        }
        
        List<Licao> licoes = null;
        try {
            XMLPullParserHandler parser = new XMLPullParserHandler();
            licoes = parser.parseLicao(getAssets().open("licao" + licaoid + ".xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        titulo = (TextView) findViewById(R.id.titulo);
        webconteudo = (WebView) findViewById(R.id.webconteudo);  
        
        WebSettings webSettings = webconteudo.getSettings();
        webSettings.setJavaScriptEnabled(true);
        //webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setDatabaseEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webconteudo.setWebChromeClient(new WebChromeClient());
        webconteudo.setWebViewClient(new WebViewClient());
        
        for (Licao licao : licoes)
        {
        	titulo.setText(licao.getTitulo());
        	webconteudo.loadDataWithBaseURL("file:///android_asset/", licao.getConteudo(), "text/html", "UTF-8", null);
        }
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featuredId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.sair:
			 new StandardDialog(this, uiMsgHandler, "Sair",
			            "Deseja Sair?", StandardDialog.dlgTypeYesNo, 0);
			return true;
		case R.id.sobre:
			new StandardDialog(this, uiMsgHandler, "",
		            "", StandardDialog.dlgTypeAbout, 0);
			return true;
		}
		return false;
	}

	@SuppressLint("HandlerLeak")
	private Handler uiMsgHandler = new Handler() {
	    @Override
	    public void handleMessage(Message msg) {
	        if (msg != null) {
	            if (msg.what == (StandardDialog.dlgResultYes)) {
	            	finish();
	            }
	        }
	    }
	};
	
	public void btnVoltar(View view)
	{
		Intent intent = new Intent(getBaseContext(), MainActivity.class);
		startActivity(intent);			
		finish();	
	}
	
	@SuppressWarnings("unused")
	private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
